package com.eight.cloud.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.eight.cloud.admin.entity.RolePermissionDeptEntity;
import com.eight.cloud.admin.entity.RolePermissionEntity;
import com.eight.cloud.admin.mapper.RolePermissionDeptMapper;
import com.eight.cloud.admin.mapper.RolePermissionMapper;
import com.eight.cloud.admin.service.IDeptService;
import com.eight.cloud.admin.service.IRolePermissionDeptService;
import com.eight.cloud.core.global.constant.DataScopeConstants;
import com.eight.cloud.core.global.model.LoginUser;
import com.eight.cloud.core.global.model.SysPermission;
import com.eight.cloud.core.global.model.SysRole;
import com.eight.cloud.mybatisplus.entity.BaseEntity;
import com.eight.cloud.mybatisplus.service.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static com.eight.cloud.core.global.constant.NumConstant.ZERO;

/**
 * @ClassName: IRoleDeptServiceImpl
 * @Author: TXC
 * @Date: 2024-10-15 15:46
 **/
@Service
public class RolePermissionDeptServiceImpl extends BaseService<RolePermissionDeptMapper, RolePermissionDeptEntity> implements IRolePermissionDeptService {
    @Resource
    private RolePermissionMapper rolePermissionMapper;
    @Resource
    private IDeptService deptService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void flush(String rolePermissionId, List<String> deptIds) {
        List<RolePermissionDeptEntity> roleDeptList =
                baseMapper.selectList(lambdaQueryWrapper().eq(RolePermissionDeptEntity::getRolePermissionId, rolePermissionId));
        Map<String, RolePermissionDeptEntity> roleDeptMap =
                roleDeptList.stream().collect(Collectors.toMap(RolePermissionDeptEntity::getDeptId, e -> e));
        deptIds.forEach(deptId -> {
            RolePermissionDeptEntity roleDept = roleDeptMap.get(deptId);
            if (ObjectUtil.isNotNull(roleDept)) {
                roleDeptMap.remove(deptId);
            } else {
                RolePermissionDeptEntity roleDeptEntity = new RolePermissionDeptEntity();
                roleDeptEntity.setRolePermissionId(rolePermissionId);
                roleDeptEntity.setDeptId(deptId);
                baseMapper.insert(roleDeptEntity);
            }
        });
        roleDeptMap.forEach((k, v) -> removeByUuid(v.getUuid()));
    }

    @Override
    public List<String> getDeptIds(RolePermissionEntity rolePermission) {
        if (DataScopeConstants.DATA_SCOPE_CUSTOM.equals(rolePermission.getDataScope())) {
            return baseMapper.selectList(lambdaQueryWrapper().eq(RolePermissionDeptEntity::getRolePermissionId,
                    rolePermission.getUuid())).stream().map(BaseEntity::getUuid).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public List<String> getRolePermissionDeptIds(List<RolePermissionEntity> rolePermissions, LoginUser loginUser) {
        if (ObjectUtil.isNotEmpty(rolePermissions)) {
            return rolePermissions.stream()
                    .map(sysPermission -> getDeptIds(sysPermission, loginUser))
                    .filter(Objects::nonNull).flatMap(List::stream).distinct()
                    .collect(Collectors.toList());
        }
        return Collections.emptyList();
    }

    @Override
    public String getRolePermissionDataScope(List<RolePermissionEntity> rolePermissions) {
        if (ObjectUtil.isNotEmpty(rolePermissions)) {
            Map<String, List<RolePermissionEntity>> dataScopeMap = rolePermissions.stream().filter(e -> ObjectUtil.isNotEmpty(e.getDataScope()))
                    .collect(Collectors.groupingBy(RolePermissionEntity::getDataScope, Collectors.toList()));
            if (dataScopeMap.containsKey(DataScopeConstants.DATA_SCOPE_ALL)) {
                return DataScopeConstants.DATA_SCOPE_ALL;
            } else if (dataScopeMap.containsKey(DataScopeConstants.DATA_SCOPE_CUSTOM) || dataScopeMap.containsKey(DataScopeConstants.DATA_SCOPE_DEPT) || dataScopeMap.containsKey(DataScopeConstants.DATA_SCOPE_DEPT_AND_CHILD)) {
                return DataScopeConstants.DATA_SCOPE_DEPT_AND_CHILD;
            } else if (dataScopeMap.containsKey(DataScopeConstants.DATA_SCOPE_SELF)){
                return DataScopeConstants.DATA_SCOPE_SELF;
            }else {
                return null;
            }
        }
        return null;
    }

    @Override
    public List<String> getDeptIds(RolePermissionEntity sysPermission, LoginUser loginUser) {
        if (DataScopeConstants.DATA_SCOPE_CUSTOM.equals(sysPermission.getDataScope())) {
            return getDeptIds(sysPermission);
        } else if (DataScopeConstants.DATA_SCOPE_DEPT.equals(sysPermission.getDataScope())) {
            return Collections.singletonList(loginUser.getDeptId());
        }else if (DataScopeConstants.DATA_SCOPE_DEPT_AND_CHILD.equals(sysPermission.getDataScope())) {
            return deptService.getDeptAndChild(loginUser.getDeptId()).get(ZERO).obtainGetSubNode()
                    .stream().map(v -> (String)v).collect(Collectors.toList());
        }else {
            return Collections.emptyList();
        }
    }
}
